package com.gushenge.core.requests

import android.util.Log
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.httpSign
import com.gushenge.core.time
import com.gushenge.core.toast
import com.rxlife.coroutine.RxLifeScope
import rxhttp.awaitResult
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

object LiveRequest {
    //直播配置 im参数*
    fun getConfig(listener: LiveConfigBean?.()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.postForm(GlobalConsts.LiveConfig)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<LiveConfigBean>>()
                .awaitResult {
                    if (it.code==1){
                        MMKVConsts.imOpen =  !(it.data?.live_appid==0||it.data?.live_key.isNullOrBlank())
                        it.data?.listener()
                    }else{
                        MMKVConsts.imOpen = false
                        it.message.toast()
                    }
                    Log.e("TAG", "getConfig: "+MMKVConsts.imOpen )
                }
                .onFailure {
                    Log.e("TAG", "getConfig11: "+MMKVConsts.imOpen +it.localizedMessage)
                    MMKVConsts.imOpen = false
                    listener.invoke(null)
                }
        }
    }

    // 获取用户live_uid 和 live_sign
    fun geiLiveUserUid(listener: LiveConfigBean?.()->Unit){
        RxLifeScope().launch {
              RxHttp.postForm(GlobalConsts.LiveLogin)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<LiveConfigBean>>()
                .awaitResult {
                    if (it.code==1){
                        // 直接在这里存了
                        MMKVConsts.liveUid = it.data?.live_uid!!
                        it.data?.listener()
                    }else{

//                        if(it.message.contains("手机")){
                            it.message.toast()
//                        }
                        // 此处不在提示   要不影响未登录用户  code 100 会提示不存在
//                        it.message.toast()
                    }
                }.onFailure {
                    listener.invoke(null)
                }
        }
    }

    // 直播列表
    fun geiLiveList( p : Int ,keyword :String? = "",listener: Codes<LiveRoomBean>?.()->Unit){
        RxLifeScope().launch {
             RxHttp.postForm(GlobalConsts.LiveList)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("keyword", keyword)
                .add("p", p)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Codes<LiveRoomBean>>()
                .awaitResult {
                    if (it.code==1){
                        it?.listener()
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                    listener.invoke(null)
                }
        }
    }

    // 直播详情
    fun geiLiveInfo(room_id :String  , listener: LiveRoomInfo?.()->Unit ){
        RxLifeScope().launch {
              RxHttp.postForm(GlobalConsts.LiveInfo)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("room_id", room_id)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<LiveRoomInfo>>()
                .awaitResult {
                    if (it.code==1){
                        it.data?.listener()
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                    listener.invoke(null)
                }
        }
    }
    //礼包
    fun roomGift(roomId:String,listener : ArrayList<RoomGiftList>.() -> Unit){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.LiveGift)
                .add("room_id",roomId)
                .add("uid",MMKVConsts.uid)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Codes<RoomGiftList>>()
                .awaitResult {
                    if (it.code == 1){
                        it.data?.listener()
                    }else{
                        it.message.toast()
                    }
                }
                .onFailure {
                    Log.e("roomGift1: ",it.toString() )
                }
        }
    }
    // 发礼物
    fun roomGiftSend(type:Int,id:Int,uids:String,room_id:String,num:Int,listener : () -> Unit){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.LiveGiftSend)
                .add("id",id)
                .add("uids",uids)
                .add("uid",MMKVConsts.uid)
                .add("type",type)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("room_id",room_id)
                .add("num",num)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<String>>()
                .awaitResult {
                    Log.e("roomGiftSend: ",it.toString() )
                    if (it.code == 1){
                        listener()
                    }else{
                        it.message.toast()
                    }
                }
                .onFailure {
                    Log.e("roomGiftSend: ",it.toString() )
                }
        }
    }
    // 查询余额
    fun yuE(listener: YuE.()->Unit){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.LiveYuE)
                .add("uid",MMKVConsts.uid)
                .add("t", time)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<YuE>>()
                .awaitResult{
                    Log.e("applyWorkerInfo: ",it.toString() )
                    if (it.code==1){
                        it.data?.listener()
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                }
        }
    }
    //排行帮
    //rooms_id 直播间room_id 或者ID
    //type  1周贡献 2月贡献
    fun getLivePaiHang(type:Int,rooms_id:String , listener: Codes<LivePaiHangBean>.()->Unit){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.LIVEPAIHANG)
                .add("uid",MMKVConsts.uid)
                .add("t", time)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("rooms_id", rooms_id)
                .add("type", type)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Codes<LivePaiHangBean>>()
                .awaitResult{
                    Log.e("applyWorkerInfo: ",it.toString() )
                    if (it.code==1){
                        it.listener()
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                }
        }
    }
    //排行配置
    fun getLivePaiHangConfig( listener: ArrayList<LivePaiHangConfig>.()->Unit){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.PAIHANGCONFIG)
                .add("uid",MMKVConsts.uid)
                .add("t", time)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Codes<LivePaiHangConfig>>()
                .awaitResult{
                    Log.e("applyWorkerInfo: ",it.toString() )
                    if (it.code==1){
                        it.data?.listener()
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("applyWorkerInfo: ",it.localizedMessage )
                }
        }
    }
    // 关注
    fun liveGuanzhu(rooms_id:String , listener: Int.()->Unit ){
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.LIVEGUANZHU)
                .add("uid",MMKVConsts.uid)
                .add("t", time)
                .add("language", MMKVConsts.language)
                .add("rooms_id", rooms_id)
                .add("member_id", MMKVConsts.sub)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<String>>()
                .awaitResult{
                    Log.e("applyWorkerInfo: ",it.toString() )
                    if (it.code==1){
                        it.code?.listener()
                    }else{
                        it.message.toast()
                        it.code?.listener()
                    }
                }.onFailure {
                    Log.e("applyWorkerInfo: ",it.localizedMessage )
                    0.listener()
                }
        }
    }
    // 收藏列表
    fun getShouCang(listener: ArrayList<LiveRoomBean>?.()->Unit){
        RxLifeScope().launch {
              RxHttp.postForm(GlobalConsts.LIVESHOUCANGLIST)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Codes<LiveRoomBean>>()
                .awaitResult {
                    if (it.code==1){
                        listener(it.data)
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "sendDongTai: ${it.localizedMessage}", )
                    "获取收藏列表失败，请重试".toast()
                }
        }
    }
    // 钻石余额和收益
    fun getWallet(listener: CodeWallet?.()->Unit){
        RxLifeScope().launch {
              RxHttp.postForm(GlobalConsts.LIVEwallet)
                .add("language", MMKVConsts.language)
                .add("member_id", MMKVConsts.sub)
                .add("uid", MMKVConsts.uid)
                .add("t", time)
                .add("sign", httpSign(MMKVConsts.uid))
                .toClass<Code<CodeWallet>>()
                .awaitResult {
                    if (it.code==1){
                        listener(it.data)
                    }else{
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "sendDongTai: ${it.localizedMessage}", )
                    "获取钻石余额和收益失败，请重试".toast()
                }
        }
    }
}